/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package persistencia.dao.hibernate;

import java.util.List;
import org.hibernate.Session;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import persistencia.pojo.Ticket;

/**
 *
 * @author Victor
 */
public class TicketDAOImpl {
    public void saveTicket(Session session,Ticket ticket){
        try {
            session.save(ticket);
        } catch (HibernateException he) {
            he.printStackTrace();
        }

    }
    public void updateTicket(Session session,Ticket ticket){
        try {
            session.update(ticket);
        } catch (HibernateException he) {
            he.printStackTrace();
        }

    }
    public void deleteTicket(Session session,Ticket ticket){
        try {
            session.delete(ticket);
        } catch (HibernateException he) {
            he.printStackTrace();
        }

    }
    public Ticket getTicket(Session session,String codticket){
        Ticket retorno=null;
        try {
            retorno= (Ticket) session.get(Ticket.class,codticket);
        } catch (HibernateException he) {
            he.printStackTrace();
        }
        return retorno;
    }    
    public List<Ticket> getAllTickets(Session session){
        List<Ticket> retorno=null;
        try {
            Query q = session.createQuery("from Ticket");
            List<Ticket> resultList = q.list();
            retorno = resultList;
        } catch (HibernateException he) {
            he.printStackTrace();
        }
        return retorno;
    }
    public List<Ticket> getTicketsWhere(Session session, String strColTabla, String strValueCol, String strPagActual){
        List<Ticket> retorno=null;
        try {
            Query q = session.createQuery("from Ticket where "+strColTabla+" like '"+strValueCol+"%'");
            List<Ticket> resultList = q.list();
            retorno = resultList;
        } catch (HibernateException he) {
            he.printStackTrace();
        }
        return retorno;
    }
    
    
    public List<Ticket> getTicketsAsignadoSoporte(Session session, String co_soporte){
        List<Ticket> retorno=null;
        try {
            Query q = session.createQuery("from Ticket where co_soporte='"+co_soporte+"' AND estado='T'");
            List<Ticket> resultList = q.list();
            retorno = resultList;
        } catch (HibernateException he) {
            he.printStackTrace();
        }
        return retorno;
    }
    
    public String idTicket(Session session){
        String retorno=null;
        try {
            Query q = session.createSQLQuery("SELECT CONCAT('T',YEAR(NOW()),MONTH(NOW()),IFNULL(RIGHT(CONCAT('00',SUBSTRING(MAX(co_ticket),8)+1),3),'001')) FROM ticket t WHERE co_ticket LIKE CONCAT('T',YEAR(NOW()),MONTH(NOW()),'%')");
            List resultado = q.list();
            retorno = (String)resultado.get(0);
        } catch (HibernateException he) {
            he.printStackTrace();
        }
        return retorno;
    }
}
